//
//  JXHPhotoBrowserAnimator.swift
//  JXHSinaWeiBo
//
//  Created by juxiaohui on 16/11/15.
//  Copyright © 2016年 juxiaohui. All rights reserved.
//

import UIKit

class JXHPhotoBrowserAnimator: NSObject {

    var isPresented  : Bool = false
    
    var presentDelegate  : JXHPhotoBrowserAnimatorPresentDelegate?
    
    var dissmissDelegate : JXHPhotoBrowserAnimatorDissmissDelegate?
    
    var indePath : IndexPath?
}

// 面向协议开发

protocol JXHPhotoBrowserAnimatorPresentDelegate : NSObjectProtocol {
    func startRect(indexPath : IndexPath) -> CGRect?
    func endRect(indexPath : IndexPath) -> CGRect?
    func imageView(indexPath : IndexPath) -> UIImageView?
}

protocol JXHPhotoBrowserAnimatorDissmissDelegate : NSObjectProtocol {
    func indexPathForDissmissView() -> IndexPath?
    func imageView() -> UIImageView?
}

extension JXHPhotoBrowserAnimator : UIViewControllerTransitioningDelegate {
   
    func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresented = true
        return self
    }
    
    func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresented = false
        return self
    }
}

extension JXHPhotoBrowserAnimator : UIViewControllerAnimatedTransitioning {
    
    func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
        return 0.5
    }
    
    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
        isPresented ? animationForPresentedView(using: transitionContext) : animationForDissmissView(using: transitionContext)
    }
    
    //弹出
    func animationForPresentedView(using transitionContext: UIViewControllerContextTransitioning)  {
        
        guard let presentDelegate = presentDelegate, let indePath = indePath else {
            return
        }
        
        //取出弹出的View
        let presentView = transitionContext.view(forKey: .to)!
        
        //将presentView 添加到容器
        transitionContext.containerView.addSubview(presentView)
        
        transitionContext.containerView.backgroundColor = UIColor.black
        
        //获取执行动画的图片
        
        let start = presentDelegate.startRect(indexPath: indePath)
        
        let imageView = presentDelegate.imageView(indexPath: indePath)
        
        transitionContext.containerView.addSubview(imageView!)
        imageView?.frame = start!
        
        //执行动画
        presentView.alpha = 0
        UIView.animate(withDuration: transitionDuration(using: transitionContext), animations: {
            
            imageView?.frame = presentDelegate.endRect(indexPath: indePath)!
        }) { (_) in
            transitionContext.containerView.backgroundColor = UIColor.clear
            imageView?.removeFromSuperview()
            presentView.alpha = 1.0
            transitionContext.completeTransition(true)
        }
    }
    
    //消失
    func animationForDissmissView(using transitionContext: UIViewControllerContextTransitioning)  {
        
        guard let dissmissDelegate = dissmissDelegate, let presentDelegate = presentDelegate else {
            return
        }
        //取出消失的View
        let dissmissView = transitionContext.view(forKey: .from)!
        dissmissView.removeFromSuperview()
        
        //获取执行动画的ImageView
        
        let imageView = dissmissDelegate.imageView()!
        let indexPath = dissmissDelegate.indexPathForDissmissView()
         transitionContext.containerView.addSubview(imageView)
        
        //执行动画
        UIView.animate(withDuration: transitionDuration(using: transitionContext), animations: {
            imageView.frame = presentDelegate.startRect(indexPath: indexPath!)!
        }) { (_) in
            transitionContext.completeTransition(true)
        }
    }
}
